import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path:'/changePass',
      component:() => import('@/views/ChangPass.vue')
    },
    {
      path: '/login',
      component: () => import('@/views/Login.vue'),
      meta:{
        auth:true
      }
    },
    {
      path:'/register',
      component:() =>import('@/views/Register.vue'),
      meta:{
        auth:true
      }
    },
    {
      name:'home',
      path:'/',
      component:() => import("@/views/Home.vue")
    }
  ],
})


router.beforeEach((to,from,next) =>{
  const token = sessionStorage.getItem('share-data') != null ? JSON.parse(sessionStorage.getItem('share-data') || '').token:''
  if(token !== '' || to.meta.auth){
    next()
  }else{
    next('/login')
  }
})

export default router
